package com.kun.feign.interceptor;

import cn.hutool.core.util.StrUtil;
import com.kun.constant.SecurityConstant;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 令牌中继: 用户各个资源服务之间传递认证token
 *
 * @author kun.li
 */
@Slf4j
public class AccessTokenInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String accessToken = request.getHeader(SecurityConstant.AUTHORIZATION);
        if (StrUtil.isNotEmpty(accessToken)) {
            template.header(SecurityConstant.AUTHORIZATION, accessToken);
        }
    }
}
